package org.ccay.async.dispatcher;

import org.ccay.async.AsyncMessage;
import org.ccay.async.IMessageSender;
import org.ccay.core.dispatcher.IEventArgs;
import org.ccay.core.dispatcher.IEventHandler;
import org.ccay.core.ioc.Ccay;

/**
 * 异步事件触发器	
 * 
 * @since 2012-7-4
 */
public final class AsyncEventUtil {
	
	/**
	 * Avoid Instances
	 */
	private AsyncEventUtil(){
	}
	
	private static final IMessageSender messageSender = Ccay.getContext().getBean("messageSender",IMessageSender.class);
	
	/** 
	 * 	
	 * 
	 * @since 2012-7-4
	 * @param handlerClass
	 * @param args
	 * @param ignoreError
	 */
	public static <T extends IEventHandler<E>,E extends IEventArgs> void dispatch(Class<T> handlerClass, E args, boolean ignoreError){
		EventArgsPack<T,E> ep = new EventArgsPack<T,E>();
		
		ep.setEventArgs(args);
		ep.setHandlerClass(handlerClass);
		ep.setIgnoreError(ignoreError);
		
		AsyncMessage message = new AsyncMessage("AsyncEvent");
		message.setContent(ep);
		
		messageSender.send(message);
	}
}
